package com.jzo2o.foundations.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-16
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
        private ServeItemServiceImpl serveItemService;
    @Override
    public PageResult<ServeResDTO> page1(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, () -> baseMapper.selectPage1(dto.getRegionId()));
    }

    @Override
    @Transactional
    public void addBatch(List<ServeUpsertReqDTO> dtoList) {
        if (CollUtil.isEmpty(dtoList)){
            throw new ForbiddenOperationException("添加的服务项不能为空");
        }
        for (ServeUpsertReqDTO dto : dtoList) {
            ServeItem serveItem = serveItemService.getById(dto.getServeItemId());
            if (serveItem==null||serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("服务项不存在或未启用");
            }
            Serve one = this.lambdaQuery().eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .one();
            if (one!=null){
                throw new ForbiddenOperationException("服务项已存在:"+serveItem.getName());
            }
            Serve serve = BeanUtil.copyProperties(dto, Serve.class);
            boolean save = this.save(serve);
            if (!save){
                throw new ForbiddenOperationException("添加失败");
            }

        }

    }

    @Override
    public void onSale(Long id) {
        Serve serve = this.getById(id);
        if (serve==null||!(serve.getSaleStatus()== FoundationStatusEnum.INIT.getStatus()||
        serve.getSaleStatus()== FoundationStatusEnum.DISABLE.getStatus())){
            throw new ForbiddenOperationException("服务不存在或已上架");
        }
        ServeItem serveItem = serveItemService.getById(serve.getServeItemId());
        if (serveItem==null||serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项不存在或未启用");
        }
        boolean update = this.lambdaUpdate().
                eq(Serve::getId, id).
                set(Serve::getSaleStatus,
                        FoundationStatusEnum.ENABLE.getStatus()).update();
        if (!update){
            throw new ForbiddenOperationException("上架失败");
        }
    }
}
